package com.service;

import cn.hutool.Hutool;
import cn.hutool.core.util.IdUtil;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;

/**
 * Description:
 *
 * @Autor: Rolin
 * datetime: 04-27-10:05
 */
@Service
//@DefaultProperties(defaultFallback = "payment_Global_fallBack")
public class HystrixService {

    //正常测试方法
    public String hystrix_ok(Integer id) {
        return "当前线程名:" + Thread.currentThread().getName() + "\t" + "id:" + id;
    }


    //测试超时测试方法：
    @HystrixCommand(fallbackMethod = "payment_timeoutHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    })
    public String hystrix_timeout(Integer id) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "线程池:" + Thread.currentThread().getName() + "paymentInfo_TimeOut,id: " + id + "\t" + "O(∩_∩)O，耗费3秒";
    }

    public String payment_timeoutHandler(Integer id) {
        return "线程名:" + Thread.currentThread().getName() + "专属服务降级O(∩_∩)O哈哈~" + "id=" + id;
    }

    //**********************此处演示熔断状态*************
    @HystrixCommand(fallbackMethod = "test02", commandProperties = {
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true")

    })
    public String circuitBreak(Integer num) {
        if (num < 0) {
            throw new RuntimeException();
        }
        //糊涂工具包进行随机生成序列号
        String serial = IdUtil.randomUUID();
        return Thread.currentThread().getName() + "\t运行成功，O(∩_∩)O哈哈，number=" + num + "\t" + serial;
    }

    public String test02(Integer num) {
        return "出错来啦啦啦啦啊,num=" + num;
    }

}
